import { getQuestionListService } from '../services/question';
import { useSearchParams } from 'react-router-dom';
import { useRequest } from 'ahooks';
import {
  LIST_SEARCH_PARAM_KEY,
  LIST_PAGE_KEY,
  LIST_PAGE_SIZE_KEY,
  LIST_PAGE_SIZE,
} from '../constants/index';
type OptionType = {
  isStar: boolean;
  isDeleted: boolean;
  page: number;
  pageSize: number;
};
function useLoadQuestionListData(opt?: Partial<OptionType>) {
  const [searchParams] = useSearchParams();

  const { loading, data, error, refresh } = useRequest(
    async () => {
      const keyword = searchParams.get(LIST_SEARCH_PARAM_KEY) || '';
      const page = parseInt(searchParams.get(LIST_PAGE_KEY) || '') || 1;
      const pageSize = parseInt(searchParams.get(LIST_PAGE_SIZE_KEY) || '') || LIST_PAGE_SIZE;
      const data = await getQuestionListService({ keyword, page, pageSize, ...opt });
      return data;
    },
    {
      refreshDeps: [searchParams],
    },
  );
  return { loading, data, error, refresh };
}
export default useLoadQuestionListData;
